
package de.gratnik.sensors;

import java.security.InvalidParameterException;


import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class DeviceInfoHelper {

  private static String TAG = "DeviceInfoHelper";

  private static int screenWidth;

  private static int screenHeight;

  private static boolean _isPortrateMode;

  static void reviewScreenProperties(Context context) {
    if (context == null)
      throw new InvalidParameterException("Context cannot be NULL!");

    Display displayMetrics = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    screenWidth = displayMetrics.getWidth();
    screenHeight = displayMetrics.getHeight();
    _isPortrateMode = displayMetrics.getOrientation() == Configuration.ORIENTATION_PORTRAIT;

    Log.v(TAG, "Screen width: " + screenWidth);
    Log.v(TAG, "Screen height: " + screenHeight);
    Log.v(TAG, "is Portrate mode: " + _isPortrateMode);
  }

  public static boolean IsPortrateMode(Context context) {
	  reviewScreenProperties(context);
    return _isPortrateMode;
  }

  public static int getScreenWidth(Context context) {
	  reviewScreenProperties(context);
    return screenWidth;
  }

  public static int getScreenHeight(Context context) {
	  reviewScreenProperties(context);
    return screenHeight;
  }

  public static int getScreenOrientation(final WindowManager windowManager) {
    Display getOrient = windowManager.getDefaultDisplay();
    if (getOrient.getWidth() == getOrient.getHeight()) {
      return Configuration.ORIENTATION_LANDSCAPE;
    } else {
      if (getOrient.getWidth() < getOrient.getHeight()) {
        return Configuration.ORIENTATION_PORTRAIT;
      } else {
        return Configuration.ORIENTATION_LANDSCAPE;
      }
    }
  }

  
  public static boolean isTablet(Context context, double minimumScreenSize) {
    try {
      // Compute screen size
      DisplayMetrics dm = context.getResources().getDisplayMetrics();
      float screenWidth = dm.widthPixels / dm.xdpi;
      float screenHeight = dm.heightPixels / dm.ydpi;
      double size = Math.sqrt(Math.pow(screenWidth, 2) + Math.pow(screenHeight, 2));
      // Tablet devices should have a screen size greater than
      // "minimumScreenSize" inches
      return size >= minimumScreenSize;
    } catch (Throwable t) {
      Log.e(TAG, "Failed to compute screen size", t);
      return false;
    }
  }

  
}
